package trlygjj.tanruiliyigenjinjin.web.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import trlygjj.tanruiliyigenjinjin.domain.Employee;
import trlygjj.tanruiliyigenjinjin.page.PageResult;
import trlygjj.tanruiliyigenjinjin.query.EmployeeQueryObject;
import trlygjj.tanruiliyigenjinjin.service.IEmployeeService;
import trlygjj.tanruiliyigenjinjin.util.JsonResult;
import trlygjj.tanruiliyigenjinjin.util.PermissionName;
import trlygjj.tanruiliyigenjinjin.util.SignInException;
import trlygjj.tanruiliyigenjinjin.util.UploadUtil;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by user on 2018/7/11.
 */
@Controller
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    public ServletContext cxt;

    /**
     *根据员工id查询角色
     * @param employeeId 传回来的员工id
     * @return 返回封装了角色id的集合
     */
    @RequestMapping("selectRoleByEmployeeId")
    @ResponseBody
    @PermissionName("根据员工id查角色")
    @RequiresPermissions("employee:selectRoleByEmployeeId")
    public List<Long> selectRoleByEmployeeId(Long employeeId){
        return  employeeService.selectRoleByEmployeeId(employeeId);
    }
    /**
     * 提供一个默认不输入后缀,直接可以跳转到具有分页的列表页面
     * @return
     */
    @RequestMapping("")
    @PermissionName("默认员工跳转")
    @RequiresPermissions("employee:index")
    public String index(){
        return "employee/list";
    }

    /**
     * 具有分页和高级查询
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    @PermissionName("带分页的员工数据")
    @RequiresPermissions("employee:list")
    public PageResult list(EmployeeQueryObject qo){
        PageResult result = employeeService.query(qo);
        return result;
    }

    /**
     * 查询全部
     * @return
     */
    @RequestMapping("listAll")
    @ResponseBody
    @PermissionName("查询全部员工")
    @RequiresPermissions("employee:listAll")
    public List<Employee> listAll(){
        return employeeService.selectAll();
    }

    /**
     * 删除
     * @param employeeId 要删除的员工对象id
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequestMapping("delete")
    @ResponseBody
    @PermissionName("删除员工")
    @RequiresPermissions("employee:delete")
    public JsonResult delete(Long employeeId){
        JsonResult result = new JsonResult();
        try{
            employeeService.deleteByPrimaryKey(employeeId);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

    /**
     *更新
     * @param employee 要更新的员工对象
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequestMapping("update")
    @ResponseBody
    @PermissionName("修改员工")
    @RequiresPermissions("employee:update")
    public JsonResult update(Employee employee){
        /*if (pic != null) {
            // 如果有传图片进来,就执行添加操作
            // 判断之前是否有图片,如果有就先删除之前的在添加
            if (employee.getImagepath() != null&&!"".equals(employee.getImagepath().trim())) {
                UploadUtil.deleteFile(
                        cxt, employee.getImagepath());
            }
            String realPath = cxt.getRealPath("/upload");
            String imgPath = UploadUtil.upload(pic, realPath);
            employee.setImagepath(imgPath);
        }*/
        JsonResult result = new JsonResult();
        try{
            employeeService.updateByPrimaryKey(employee);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

    /**
     * 提供一个方法专门用来上传图片的
     * @param
     * @param pic
     * @return
     */
    @RequestMapping("uploadImage")
    @ResponseBody
    @PermissionName("修改身份证图片")
    @RequiresPermissions("employee:uploadImage")
    public JsonResult uploadImage(String hiddenImagePath, Long employeeId, MultipartFile pic, HttpServletRequest request){
        Employee employee = employeeService.selectByPrimaryKey(employeeId);
        JsonResult result = new JsonResult();
        try{
            if(employee != null){
                if (pic != null && !pic.isEmpty() && pic.getSize() != 0) {
                    // 如果有传图片进来,就执行添加操作
                    // 判断之前是否有图片,如果有就先删除之前的在添加
                    if (employee.getImagepath() != null && !"".equals(employee.getImagepath().trim())) {
                        UploadUtil.deleteFile(
                                cxt, employee.getImagepath());
                    }
                    String realPath = cxt.getRealPath("/upload");
                    String imgPath = UploadUtil.upload(pic, realPath);
                    employee.setRealpath(request.getServletContext().getRealPath(imgPath));
                    //提供一个根据员工id只更新图片路径的方法
                    employeeService.uploadImage(imgPath, employeeId);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }


    /**
     * 保存
     * @param employee 要保存的员工对象
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequestMapping("save")
    @ResponseBody
    @PermissionName("保存员工")
    @RequiresPermissions("employee:save")
    public JsonResult save(Employee employee){
        JsonResult result = new JsonResult();
        try{
            employeeService.insert(employee);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }



    /**
     * 根据岗位来查询员工
     * @return 员工的名称
     */
    @RequestMapping("selectUsernameByJobs")
    @ResponseBody
    @PermissionName("根据职业查询员工")
    @RequiresPermissions("employee:selectUsernameByJobs")
    public  List<Employee> selectUsernameByJobs(String  jobs, HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println(jobs);
        String s = new String(request.getParameter("jobs").getBytes("ISO8859-1"), "UTF-8");
        System.out.println(s);

        List<Employee>  list= employeeService.selectUsernameByJobs(s);

        return list;
    }



    @RequestMapping("changeState")
    @RequiresPermissions("employee:changeState")
    @PermissionName("员工状态")
    @ResponseBody
    public JsonResult changeState(Long id){
        JsonResult result = new JsonResult();
        try {
            employeeService.changeState(id);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("操作失败");
        }
        return result;
    }
    @RequestMapping("changeAdmin")
    @RequiresPermissions("employee:changeAdmin")
    @PermissionName("管理员状态")
    @ResponseBody
    public JsonResult changeAdmin(Long id){
        JsonResult result = new JsonResult();
        try {
            employeeService.changeAdmin(id);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("操作失败");
        }
        return result;
    }


    /**
     * 保存
     * @param employeeId 需要修改状态的员工id
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequestMapping("updateState")
    @ResponseBody
    @PermissionName("修改员工状态")
    @RequiresPermissions("employee:updateState")
    public JsonResult updateState(Long employeeId){
        JsonResult result = new JsonResult();
        try{
            employeeService.updateState(employeeId);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

    @RequestMapping("getCurrentEmployee")
    @ResponseBody
    public Employee getCurrentEmployee(){
        Subject subject = SecurityUtils.getSubject();
        if(subject != null && subject.getPrincipal() != null){
            return (Employee) SecurityUtils.getSubject().getPrincipal();
        }
        return null;
    }

    /**
     * 解锁功能
     * @param password 传输过来的密码
     * @return
     */

    @RequestMapping("deblocking")
    @ResponseBody

    public JsonResult deblocking(String password){
        JsonResult result = new JsonResult();
        try{
            employeeService.deblocking(password);
        }catch (SignInException e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }











}
